package com.zhentao.utils.sendmeg;


import com.aliyuncs.utils.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Controller
public class SmsController {

    @Autowired
    private SmsService smsService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @RequestMapping("/send")
    @ResponseBody
    public boolean send(@RequestParam("phone") String phone){
        System.out.println(phone);
       String code="";
        //随机6位数
        Random random = new Random();
        int randomNumber = random.nextInt(900000) + 100000;
        code=String.valueOf(randomNumber);
        //redis存储验证码
        redisTemplate.opsForValue().set("code:"+phone,code,1, TimeUnit.MINUTES);
        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        boolean b=smsService.send(phone,map);
        if(b){
            return true;
        }else{
            return false;
        }
    }
}